package com.xyz.basesdk.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xyz.basesdk.utils.MyToast;
import com.xyz.basesdk.utils.SpUtil;

/**
 * @author mac
 * on 2018/6/25.
 */
public abstract class BaseFragment extends Fragment {
    private static long LAST_CLICK_TIME = 0;
    public Context context;
    /**
     * 防止多次点击
     */
    public boolean isShake = false;
    public SpUtil sp;
    public View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        sp = SpUtil.getInstance();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), null);
        initView();
        initData();
        return view;
    }

    /**
     * 设置fragment 界面资源
     *
     * @return 返回资源id
     */
    protected abstract int getLayoutId();

    /**
     * 初始化界面,执行控件的findViewById
     */
    protected abstract void initView();

    /**
     * 当前fragment是否可见
     *
     * @param isVisible true可见,false不可见
     */
    public void fgVisible(boolean isVisible) {

    }

    /**
     * 数据上的初始化
     */
    protected abstract void initData();


    public void goActivityForResult(Class clazz, int flag) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - LAST_CLICK_TIME > backPressExitTime) {
            startActivityForResult(new Intent(BaseApplication.getContext(), clazz), flag);
        }
        LAST_CLICK_TIME = currentTimeMillis;
    }

    public void goActivityForResult(Intent intent, int flag) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - LAST_CLICK_TIME > backPressExitTime) {
            startActivityForResult(intent, flag);
        }
        LAST_CLICK_TIME = currentTimeMillis;
    }

    public void goActivity(Class clazz) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - LAST_CLICK_TIME > backPressExitTime) {
            startActivity(new Intent(BaseApplication.getContext(), clazz));
        }
        LAST_CLICK_TIME = currentTimeMillis;
    }

    public void goActivity(Intent intent) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - LAST_CLICK_TIME > backPressExitTime) {
            startActivity(intent);
        }
        LAST_CLICK_TIME = currentTimeMillis;
    }

    public void showDialog() {

    }

    public void error() {

    }

    public void showHint(String msg) {
        MyToast.showToast(msg);
    }

    public void noNet() {

    }

    public void showLoadingFull() {

    }

    public void hideLoadingFull() {

    }

    public void showLoadingPart() {
        isShake = true;
    }

    public void hideLoadingPart() {
        isShake = false;
    }
}
